package com.newland.mesdk.demo.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.newland.mesdk.demo.main.R;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.SharedPreferencesUtil;

/**
 * Created by YJF 所有模块的菜单
 */
public class MenuFragment extends Fragment implements OnClickListener {
	private LinearLayout linBuzzerModl, linLCD;
	private LinearLayout linLightModl,linKeyBoard;
	private LinearLayout linPrinterModl, linQPBOC,linRFcard;
	private LinearLayout linScannerModl, linExternalPin,linSwip;
	private Button btnTermlModl, btnBuzzerModl, btnCardReaderModl, btnLCD;
	private Button btnEMVModl, btnICCardModl, btnLightModl, btnConsume;
	private Button btnKeyBoardModl, btnPinModl, btnPrinterModl, btnQPBOC;
	private Button btnRFCardModl, btnSecurityModl, btnScannerModl, btnExternalPin;
	private Button btnStorageModl, btnSwipModl;
	private String deviceType;
	private MainActivity mainActivity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_menu, null);
		mainActivity = (MainActivity) getActivity();
		deviceType = SharedPreferencesUtil.getStringParam(view.getContext(), Const.DEVICETYPE);
		initMenuFragmentView(view);
		showModule();
		return view;
	}

	// 初始化模块按钮
	public void initMenuFragmentView(View view) {
		linBuzzerModl = (LinearLayout) view.findViewById(R.id.module_buzzer);
		
		linKeyBoard=(LinearLayout)view.findViewById(R.id.module_keyboard);

		linLCD = (LinearLayout) view.findViewById(R.id.module_lcd);

		linLightModl = (LinearLayout) view.findViewById(R.id.module_light);

		linPrinterModl = (LinearLayout) view.findViewById(R.id.module_printer);

		linQPBOC = (LinearLayout) view.findViewById(R.id.module_qpboc);

		linScannerModl = (LinearLayout) view.findViewById(R.id.module_scanner);

		linExternalPin = (LinearLayout) view.findViewById(R.id.module_externalpin);
		
		linRFcard = (LinearLayout) view.findViewById(R.id.module_rfcard);
		
		linSwip = (LinearLayout) view.findViewById(R.id.module_swiper);

		btnTermlModl = (Button) view.findViewById(R.id.terminal_manage);
		btnTermlModl.setOnClickListener(this);

		btnBuzzerModl = (Button) view.findViewById(R.id.buzzer);
		btnBuzzerModl.setOnClickListener(this);

		btnCardReaderModl = (Button) view.findViewById(R.id.cardreader);
		btnCardReaderModl.setOnClickListener(this);

		btnLCD = (Button) view.findViewById(R.id.lcd);
		btnLCD.setOnClickListener(this);

		btnEMVModl = (Button) view.findViewById(R.id.emv);
		btnEMVModl.setOnClickListener(this);

		btnICCardModl = (Button) view.findViewById(R.id.iccard);
		btnICCardModl.setOnClickListener(this);

		btnLightModl = (Button) view.findViewById(R.id.light);
		btnLightModl.setOnClickListener(this);

		btnConsume = (Button) view.findViewById(R.id.consume);
		btnConsume.setOnClickListener(this);

		btnKeyBoardModl = (Button) view.findViewById(R.id.keyboard);
		btnKeyBoardModl.setOnClickListener(this);

		btnPinModl = (Button) view.findViewById(R.id.pin);
		btnPinModl.setOnClickListener(this);

		btnPrinterModl = (Button) view.findViewById(R.id.printer);
		btnPrinterModl.setOnClickListener(this);

		btnQPBOC = (Button) view.findViewById(R.id.qpboc);
		btnQPBOC.setOnClickListener(this);

		btnRFCardModl = (Button) view.findViewById(R.id.rfcard);
		btnRFCardModl.setOnClickListener(this);

		btnSecurityModl = (Button) view.findViewById(R.id.security);
		btnSecurityModl.setOnClickListener(this);

		btnScannerModl = (Button) view.findViewById(R.id.scanner);
		btnScannerModl.setOnClickListener(this);

		btnExternalPin = (Button) view.findViewById(R.id.externalpin);
		btnExternalPin.setOnClickListener(this);

		btnStorageModl = (Button) view.findViewById(R.id.storage);
		btnStorageModl.setOnClickListener(this);

		btnSwipModl = (Button) view.findViewById(R.id.swiper);
		btnSwipModl.setOnClickListener(this);
	}

	// 根据选择的不同设备，显示不同的模块
	public void showModule() {
		if (deviceType.equals(Const.DeviceName.IM81)) {
			linLCD.setVisibility(View.GONE);
			linScannerModl.setVisibility(View.GONE);
			linQPBOC.setVisibility(View.GONE);
		} else if (deviceType.equals(Const.DeviceName.ME30)) {
			linBuzzerModl.setVisibility(View.GONE);
			linLightModl.setVisibility(View.GONE);
			linExternalPin.setVisibility(View.GONE);
			linScannerModl.setVisibility(View.GONE);
			linPrinterModl.setVisibility(View.GONE);
		} else if (deviceType.equals(Const.DeviceName.N900)) {
			linBuzzerModl.setVisibility(View.GONE);
			linExternalPin.setVisibility(View.GONE);
			linQPBOC.setVisibility(View.GONE);
			linLCD.setVisibility(View.GONE);
			linKeyBoard.setVisibility(View.GONE);
		} else if (deviceType.equals(Const.DeviceName.ME31)) {
			linBuzzerModl.setVisibility(View.GONE);
			linLightModl.setVisibility(View.GONE);
			linExternalPin.setVisibility(View.GONE);
			linScannerModl.setVisibility(View.GONE);
		} else if (deviceType.equals(Const.DeviceName.AUDIO)) {
			linBuzzerModl.setVisibility(View.GONE);
			linLightModl.setVisibility(View.GONE);
			linExternalPin.setVisibility(View.GONE);
			linScannerModl.setVisibility(View.GONE);
			linPrinterModl.setVisibility(View.GONE);
			linLCD.setVisibility(View.GONE);
			linQPBOC.setVisibility(View.GONE);
			linKeyBoard.setVisibility(View.GONE);
			linSwip.setVisibility(View.GONE);
			linRFcard.setVisibility(View.GONE);
		}
	}

	// 选择模块
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.terminal_manage: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.TERNAL);
				mainActivity.showMessage("选择了终端管理模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.buzzer: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.BUZZER);
				mainActivity.showMessage("选择了蜂鸣器模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.cardreader: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.CARDREADER);
				mainActivity.showMessage("选择了读卡器模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.emv: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.EMV);
				mainActivity.showMessage("选择了EMV模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.lcd: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.LCD);
				mainActivity.showMessage("选择了LCD模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.iccard: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.ICCARD);
				mainActivity.showMessage("选择了IC卡模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.light: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.LIGHT);
				mainActivity.showMessage("选择了指示灯模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.consume: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.CONSUME);
				mainActivity.showMessage("选择了消费模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.keyboard: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.KEYBOARD);
				mainActivity.showMessage("选择了密码键盘模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.pin: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.PIN);
				mainActivity.showMessage("选择了PIN模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.printer: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.PRINTER);
				mainActivity.showMessage("选择了打印模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.qpboc: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.QPOBC);
				mainActivity.showMessage("选择了QPBOC模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.rfcard: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.RFCARD);
				mainActivity.showMessage("选择了非接卡模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.security: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.SECURITY);
				mainActivity.showMessage("选择了安全认证模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.scanner: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.SCANNER);
				mainActivity.showMessage("选择了扫描模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.externalpin: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.EXTERNALPIN);
				mainActivity.showMessage("选择了外接密码键盘模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.swiper: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.SWIP);
				mainActivity.showMessage("选择了刷卡模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}
		case R.id.storage: {
			if (mainActivity.isDeviceInit()) {
				SharedPreferencesUtil.setIntParam(v.getContext(), Const.CHECK_MODULE, Const.CheckedModuleName.STORAGE);
				mainActivity.showMessage("选择了存储模块", Const.MessageTag.NORMAL);
				mainActivity.switchFragment(R.id.frame, new ChildMenuFragment());
				mainActivity.isMenuFragment();
			} else {
				mainActivity.showMessage("设备未初始化，请先初始化！", Const.MessageTag.TIP);
			}

			break;
		}

		default: {
			break;
		}

		}
	}

}
